<template>
  <!-- 以下组件传值，很巧妙的利用了父组件给子组件传值时，子组件没有props相对应的设置，会默认给子组件的根元素设置这个属性 -->
  <div class="login">
    <div class="container">
      <div class="close">
        <span class="iconfont iconicon-test"></span>
      </div>
      <div class="logo">
        <span class="iconfont iconnew"></span>
      </div>
      <div class="inputs">
        <hminput
          placeholder="请输入手机号"
          type="text"
          :value="user.username"
          @input="changeUsername"
          :rules="/^1[35789]\d{9}$/"
          errmsg="请输入正确的手机号！"
        ></hminput>
        <!-- <hminput
          placeholder="请输入手机号"
          type="text"
          v-model="user.username"
        ></hminput> -->
        <hminput
          placeholder="密码"
          type="password"
          v-model="user.password"
          :rules="/^\S{3,16}$/"
          errmsg="请输入3-16位的密码！"
        ></hminput>
      </div>
      <p class="tips">
        没有账号？
        <a href="/register">去注册</a>
      </p>
      <hmbutton type="default" @click="handleLogin">登陆</hmbutton>
    </div>
  </div>
</template>

<script>
import { login } from "@/apis/user.js";
import hmbutton from "@/components/hmbutton.vue";
import hminput from "@/components/hminput.vue";
// 引入vant-Toast组件
import { Toast } from "vant";
export default {
  components: {
    hmbutton,
    hminput,
  },
  data() {
    return {
      user: {
        username: "13800138000",
        password: "123",
      },
    };
  },
  methods: {
    changeUsername(p) {
      this.user.username = p;
    },
    async handleLogin() {
      if (
        /^1[35789]\d{9}$/.test(this.user.username) &&
        /^\S{3,16}$/.test(this.user.username)
      ) {
        // login(this.user)
        //   .then((res) => {
        //     console.log(res);
        //   })
        //   .catch((err) => console.log(err));
        try {
          let res = await login(this.user);
          console.log("登陆", res);
          if (res.data.statusCode == 200) {
            // 登陆成功
            Toast.success({
              message: res.data.message,
              duration: 2000,
            });
            localStorage.setItem("hmtoutiao_token", res.data.data.token);
            localStorage.setItem("hmtoutiao_pwd", JSON.stringify(this.user));
            localStorage.setItem("user_id", res.data.data.user.id);
            let href = decodeURIComponent(window.location.href);
            href = href.split("=")[1];
            // console.log("href=", href);
            // 判断，如果是直接登录就跳到个人页面，否则登陆成功之后，根据这个参数重新跳转回文章详情页
            if (href && href != "undefined") {
              // window.location.href = href;
              this.$router.back();
            } else {
              this.$router.push({ path: `/personal/${res.data.data.user.id}` });
            }
            // console.log(this.$route.query.redirectUrl);
          }
        } catch {
          Toast.fail("用户名或密码输入有误！");
        }
      } else {
        Toast.fail("用户名或密码输入有误！");
      }
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  padding: 20px;
}
.close {
  span {
    font-size: 27 * 100vw/360;
  }
}
.logo {
  display: flex;
  justify-content: center;
  span {
    display: block;
    color: #d81e06;
    font-size: 126 * 100vw/360;
  }
}
.inputs {
  input {
    margin-bottom: 20px;
  }
}
.tips {
  margin-top: 20px;
  margin-bottom: 20px;
  text-align: right;
  a {
    color: #3385ff;
  }
}
</style>